<?php

namespace App\Http\Controllers\Backstage;

use App\AccountFlow;
use App\Department;
use App\Doctors;
use App\Exceptions\PaymentException;
use App\Exceptions\WechatException;
use App\Hospital;
use App\Services\Wechat\WechatLogin;
use App\IntegralConductNumRecord;
use App\IntegralRule;
use App\Services\UserManager;
use App\User;
use App\UserInfo;
use App\UserPayment;
use App\VipInfo;
use App\VipRule;
use App\Activity;
use App\UserActivity;
use App\UserActivityGuest;
use App\Images;
use App\CompanyInfo;
use App\UserReCompanyInfo;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;
use App\Services\FileManager;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersPayments;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class ActivityController extends Controller
{
    public function inlist(Request $request)
    {
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $page = $request->has('page')?$request->input('page'):1;
        $pagesize = $request->has('pagesize')?$request->input('pagesize'):16;
        $name = $request->has('name')?$request->input('name'):'';
        $realname = $request->has('realname')?$request->input('realname'):'';
        $cardNumber = $request->has('cardNumber')?$request->input('cardNumber'):'';
        $startTime = $request->has('startTime')?$request->input('startTime'):'';
        $endTime = $request->has('endTime')?$request->input('endTime'):'';
        $state = $request->has('state')?$request->input('state'):'';
        $status = $request->has('status')?$request->input('status'):'';
        /** @var Activity $val */
        $db = Activity::where(array())->orderBy('created_at','desc');
        $time = time();
        $timestamp = date('Y-m-d H:i:s',$time);
        if($state == '0'){//报名中
            $db = $db->where('start_time','>',$timestamp);
        }

        if($user->role == 1){
            $db = $db->where('user_id',$user->id);
        }
        if($state == 1){//正在进行
            $db = $db->where('end_time','>',$timestamp)->where('start_time','<=',$timestamp);
        }
        if($state == 2){//已结束
            $db = $db->where('end_time','<',$timestamp);
        }
        if($status){
            $db = $db->where('status',$status);
        }
        if($name){
            $db = $db->where('name', 'like', '%' . $name . '%')->whereOr('theme', 'like', '%' . $name . '%');
        }
        if($startTime && $endTime){
            $db = $db->whereBetween('created_at', [$startTime, $endTime]);
        }
        $arr = [
            'page'=>$page,
            'pagesize'=>$pagesize,
            'name'=>$name,
            'startTime'=>$startTime,
            'endTime'=>$endTime,
        ];
        $activity_list = $db->select('*')->paginate($pagesize);
        // $start = ($page - 1)<0?0:$page - 1;
        // $activity_list = $db->select('*')->offset($pagesize * $start)->limit($pagesize)->get();
        $lastPage = $activity_list->lastPage();
        $total = $activity_list->total();
        $hasMorePages = $activity_list->hasMorePages();
        $nextPageUrl = $activity_list->nextPageUrl();
        $prvePageUrl = $activity_list->previousPageUrl();
        $arr['lastPage'] = $lastPage;
        $arr['total'] = $total;
        $arr['lastPage'] = $lastPage;
        $arr['hasMorePages'] = $hasMorePages;
        $arr['nextPageUrl'] = $nextPageUrl;
        $arr['prvePageUrl'] = $prvePageUrl;
        $activity_list = $activity_list->all();
        if($activity_list){
            $timestamp = date('Y-m-d H:i:s',time());
            foreach($activity_list as $key=>$val){
                 $val->signupNum = UserActivity::where('status',1)->where('activity_id',$val->id)->count();
                 $val->shareThumb = route('Api.Image.Show',['id' => $val->share_img_id]);
                 $val->shareCodeThumb = route('Api.Image.Show',['id' => $val->qr_code_img_id]);
                 $val->signCodeThumb = route('Api.Image.Show',['id' => $val->sign_qr_code_img_id]);

                /** @var Doctors $doctor */
                $doctor = Doctors::whereUserId($val->user_id)->first();
                if($doctor){
                    if(!is_null($doctor->doc_name) && $doctor->doc_name != ''){
                        $val->username =  $doctor->doc_name;
                    }else{
                        /** @var UserInfo $userInfo */
                        $userInfo = UserInfo::whereUserId($val->user_id)->first();
                        if($userInfo){
                            if(!is_null($userInfo->nick) && $userInfo->nick != ''){
                                $val->username =  $userInfo->nick;
                            }else{
                                $val->username =  '';
                            }
                        }
                    }
                }else{
                    /** @var User $userInfo */
                    $userInfo = User::whereId($val->user_id)->first();
                    if($userInfo){
                        if(!is_null($userInfo->username) && $userInfo->username != ''){
                            $val->username =  $userInfo->username;
                        }else{
                            $val->username =  '';
                        }
                    }
                }

                $hospitalId = 0;
                $hospitalName = '';
                $depId = 0;
                $depName = '';

                $sex = $val->sexual;
                /** @var Doctors $thisDoctor */
                $thisDoctor = Doctors::whereUserId($val->user_id)->first();
                if($thisDoctor){
                    $hospital = Hospital::whereId($thisDoctor->hospital_id)->first();
                    if($hospital){
                        $hospitalId = $hospital->id;
                        $hospitalName = $hospital->name;
                    }

                    $thisDep = Department::whereId($thisDoctor->dep_id)->first();
                    if($thisDep){
                        $depId = $thisDep->id;
                        $depName = $thisDep->dep_name;
                    }

                    $sex = $thisDoctor->sexual;
                }

                $val->hospital_id = $hospitalId;
                $val->hospital_name = $hospitalName;
                $val->dep_id = $depId;
                $val->dep_name = $depName;
                $val->sex = $sex;

                 $conduct = '';
                 if($timestamp < $val->start_time){
                     $conduct = '报名中';
                 }elseif($timestamp > $val->end_time){
                     $conduct = '已结束';
                 }else{
                     $conduct = '进行中';
                 }
                 $val->activity_conduct = $conduct;

                 $val->activity_link_url = 'http://meeting.51wenyisheng.com/#/activityDetail?id='.$val->id;
            }
        }

        $arr['activity_list'] = $activity_list;
        return response()->json(['level' => 'success','message' => '获取成功','data' => $arr]);
        // return view($path, $arr);
    }

    public function insert(Request $request)
    {
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $data = $request->all();
        if(empty($data['name']) || !$data['name'])
            return response()->json(['level' => 'error','message' => '会议名称不能为空']);
        if(empty($data['startTime']) || !$data['startTime'])
            return response()->json(['level' => 'error','message' => '请选择会议开始时间']);
        if(empty($data['endTime']) || !$data['endTime'])
            return response()->json(['level' => 'error','message' => '请选择会议结束时间']);

        $cost = !empty($data['cost']) ? $data['cost'] : 0;
        if($cost < 0){
            return response()->json(['level' => 'error','message' => '金额不能小于0']);

        }

        $accommodationCost = !empty($data['accommodation_cost']) ? $data['accommodation_cost'] : 0;
        if($accommodationCost < 0)
            return response()->json(['level' => 'error','message' => '住宿费用不能小于0']);

        $integralRuleId = !empty($data['integral_rule_id']) ? $data['integral_rule_id'] : 0;

//        if($integralRuleId != 0){
//            /** @var IntegralRule $integralRule */
//            $integralRule = IntegralRule::whereType('activity')->whereId($integralRuleId)->first();
//            if(!$integralRule)
//                return response()->json(['level' => 'error','message' => '未找到该积分类型']);
//
//            //今年的开始时间和结束时间
//            $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, 1, 1, date('Y', time())));
//            $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y', time())));
//
//            $integralConductNum = IntegralConductNumRecord::whereIntegralRuleId($integralRule->id)->whereStatus(1)->whereBetween('conduct_time',[$beginTime,$endTime])->count();
//            if($integralConductNum >= $integralRule->num)
//                return response()->json(['level' => 'error','message' => '今年举办次数已满']);
//        }else{
//            $integralRule = null;
//        }

        
        $name = $data['name'];
        $startTime = $data['startTime'];
        $endTime = $data['endTime'];
        try{
            DB::beginTransaction();
            $activity = new Activity();
            $activity->name = $name;
            $activity->start_time = $startTime;
            $activity->end_time = $endTime;

            if(!empty($data['province']) && $data['province'])
                $activity->province = $data['province'];
            if(!empty($data['city']) && $data['city'])
                $activity->city = $data['city'];
            if(!empty($data['area']) && $data['area'])
                $activity->area = $data['area'];
            if(!empty($data['addressDetail']) && $data['addressDetail'])
                $activity->address_detail = $data['addressDetail'];
            if(!empty($data['rePerson']) && $data['rePerson'])
                $activity->re_person = $data['rePerson'];

            $activity->classify_id = !empty($data['classifyId']) ? $data['classifyId'] : 0;

            $activity->user_id    = $user->id;

            $activity->is_choice = !empty($data['isChoice']) ? $data['isChoice'] : 0;

            $activity->is_set_seat = !empty($data['isSetseat']) ? $data['isSetseat'] : 0;

            $activity->is_vip_free = !empty($data['isVipfree']) ? $data['isVipfree'] : 0;
            if(!empty($data['theme']) && $data['theme'])
                $activity->theme = $data['theme'];
            if(!empty($data['desc']) && $data['desc'])
                $activity->desc = $data['desc'];
            if(!empty($data['detail']) && $data['detail'])
                $activity->detail = $data['detail'];

            $activity->cost = !empty($data['cost']) ? $data['cost'] : 0;

            $activity->accommodation_cost = $accommodationCost;

            $activity->is_guest = !empty($data['isGuest']) ? $data['isGuest'] : 0;

            $activity->guest_cost = !empty($data['guestCost']) ? $data['guestCost'] : 0;

            $activity->sort = !empty($data['sort']) ? $data['sort'] : 0;

            $activity->img_id = !empty($data['imgId']) ? $data['imgId'] : 0;

            $activity->re_person_phone = !empty($data['rePersonPhone']) ? $data['rePersonPhone'] : 0;

            $activity->max_join_num = !empty($data['maxJoinNum']) ? $data['maxJoinNum'] : 0;

            $activity->is_allow_not_vip_join = !empty($data['isAllowNotVipJoin']) ? $data['isAllowNotVipJoin'] : 0;

            $activity->integral_rule_id = $integralRuleId;

            $activity->status          = 1;
            $activity->save();

            $url = "http://meeting.51wenyisheng.com/#/activitySign?id=".$activity->id;

            $qrCode = new QrCode();
            $qrCode
                ->setText($url)
                ->setSize(250)
                ->setPadding(10)
                ->setErrorCorrection('high')
                ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
                ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
                ->setImageType(QrCode::IMAGE_TYPE_PNG)
            ;

            $qrcodeContent = $qrCode->get('jpg');

            if($qrcodeContent){
                $filoInfo = FileManager::saveFileByFileContent($qrcodeContent,"qr_code_" . $activity->id . ".jpg","oss");
                $newImage = new Images();
                $newImage->file_id          = $filoInfo->id;
                $newImage->img_name         = "qr_code_" . $activity->id . ".jpg";
                $newImage->img_type         = $filoInfo->file_type;
                $newImage->save();

                $activity->sign_qr_code_img_id = $newImage->id;
                $activity->save();

                DB::commit();
                return response()->json(['level' => 'success','message' => '活动发布成功']);
            }else{
                DB::rollBack();
                return response()->json(['level' => 'error','message' => '二维码生成失败,请联系客服']);
            }
            DB::commit();
            return response()->json(['level' => 'success','message' => '会议发布成功','data' => $activity]);

        }catch (\Exception $e){
            DB::rollBack();
            return response()->json(['level' => 'error','message' => '添加失败']);
        }
    }

    public function ceshiQrCode(Request $request){

        $url = "http://meeting.51wenyisheng.com/#/activitySign?id=". 37;

        $qrCode = new QrCode();
        $qrCode
            ->setText($url)
            ->setSize(250)
            ->setPadding(10)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
            ->setImageType(QrCode::IMAGE_TYPE_PNG)
        ;

        $qrcodeContent = $qrCode->get('jpg');

        if($qrcodeContent) {
            $filoInfo = FileManager::saveFileByFileContent($qrcodeContent, "qr_code_" . $activity->id . ".jpg", "oss");
            $newImage = new Images();
            $newImage->file_id = $filoInfo->id;
            $newImage->img_name = "qr_code_" . $activity->id . ".jpg";
            $newImage->img_type = $filoInfo->file_type;
            $newImage->save();

            $url = route('Api.Image.Show',['id' => $activity->sign_qr_code_img_id]);

            return response()->json(['data' => $url]);
        }

    }

    public function delete(Request $request){

        $data = $request->input();
        if(empty($data['id']) || !$data['id'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        $id = $data['id'];
        /** @var Activity $activity */
        $activity = Activity::where('id',$id)->select('id')->first();
        if(!$activity)
           return json_encode(['level' => 'error','message' => '删除失败']);

//        $integralConductNumRecord = IntegralConductNumRecord::whereActivityId($activity->id)->first();
//        if($integralConductNumRecord){
//            $integralConductNumRecord->delete();
//        }

        $userActivityList = UserActivity::whereActivityId($activity->id)->get();
        if(count($userActivityList) > 0){
            foreach($userActivityList as $userActivity){
                $userActivity->delete();
            }
        }

        $activity->delete();


       return json_encode(['level' => 'success','message' => '删除成功']);
           
    }
    public function details(Request $request){
        $data = $request->all();
        if(empty($data['id']) || !$data['id'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        $id = $data['id'];
        /** @var Activity $activity_info */
        $activity_info = Activity::where('id',$id)->select('*')->first();
        if($activity_info){
            $activity_info->signupNum = UserActivity::where('status',1)->where('activity_id',$activity_info->id)->count();
            $activity_info->coverThumb = route('Api.Image.Show',['id' => $activity_info->img_id]);
            $activity_info->shareThumb = route('Api.Image.Show',['id' => $activity_info->share_img_id]);
            $activity_info->shareCodeThumb = route('Api.Image.Show',['id' => $activity_info->qr_code_img_id]);
            $activity_info->signCodeThumb = route('Api.Image.Show',['id' => $activity_info->sign_qr_code_img_id]);

            /** @var Doctors $doctor */
            $doctor = Doctors::whereUserId($activity_info->user_id)->first();
            if($doctor){
                if(!is_null($doctor->doc_name) && $doctor->doc_name != ''){
                    $activity_info->username =  $doctor->doc_name;
                }else{
                    /** @var UserInfo $userInfo */
                    $userInfo = UserInfo::whereUserId($activity_info->id)->first();
                    if($userInfo){
                        if(!is_null($userInfo->nick) && $userInfo->nick != ''){
                            $activity_info->username =  $userInfo->nick;
                        }else{
                            $activity_info->username =  '';
                        }
                    }
                }
            }else{
                /** @var User $userInfo */
                $userInfo = User::whereId($activity_info->user_id)->first();
                if($userInfo){
                    if(!is_null($userInfo->username) && $userInfo->username != ''){
                        $activity_info->username =  $userInfo->username;
                    }else{
                        $activity_info->username =  '';
                    }
                }
            }
       }
        $arr = [
            'activity_info'=>$activity_info,
        ];
        return response()->json(['level' => 'success','message' => '获取信息成功','data'=>$arr]);
    }


    public function update(Request $request)
    {
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if (!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $data = $request->all();
        if(empty($data['id']) || !$data['id'])
            return response()->json(['level' => 'error','message' => '会议名称不能为空']);
        if(empty($data['name']) || !$data['name'])
            return response()->json(['level' => 'error','message' => '会议名称不能为空']);
        if(empty($data['startTime']) || !$data['startTime'])
            return response()->json(['level' => 'error','message' => '请选择会议开始时间']);
        if(empty($data['endTime']) || !$data['endTime'])
            return response()->json(['level' => 'error','message' => '请选择会议结束时间']);

        $cost = !empty($data['cost']) ? $data['cost'] : 0;
        if($cost < 0){
            return response()->json(['level' => 'error','message' => '金额不能小于0']);

        }

        $accommodationCost = !empty($data['accommodation_cost']) ? $data['accommodation_cost'] : 0;
        if($accommodationCost < 0)
            return response()->json(['level' => 'error','message' => '住宿费用不能小于0']);

        $id = $data['id'];
        $name = $data['name'];
        $startTime = $data['startTime'];
        $endTime = $data['endTime'];

        $integralRuleId = !empty($data['integral_rule_id']) ? $data['integral_rule_id'] : 0;

        /** @var Activity $activity */
        $activity = Activity::where('id',$id)->first();
        if(!$activity)
            return response()->json(['level' => 'error','message' => '会议修改失败']);

//        if($integralRuleId != 0){
//            /** @var IntegralRule $integralRule */
//            $integralRule = IntegralRule::whereType('activity')->whereId($integralRuleId)->first();
//            if(!$integralRule)
//                return response()->json(['level' => 'error','message' => '未找到该积分类型']);
//
//            //今年的开始时间和结束时间
//            $beginTime = date('Y-m-d 00:00:00', mktime(0, 0,0, 1, 1, date('Y', time())));
//            $endTime = date('Y-m-d 23:39:59', mktime(0, 0, 0, 12, 31, date('Y', time())));
//
//            $integralConductNum = IntegralConductNumRecord::whereIntegralRuleId($integralRule->id)->whereStatus(1)->whereBetween('conduct_time',[$beginTime,$endTime])->count();
//            if($integralConductNum >= $integralRule->num)
//                return response()->json(['level' => 'error','message' => '今年举办次数已满']);
//        }else{
//            $integralConduct = IntegralConductNumRecord::whereActivityId($activity->id)->whereStatus(0)->first();
//            if($integralConduct){
//                $integralConduct->delete();
//            }
//
//            $integralRule = null;
//        }

        $activity->name = $name;
        $activity->start_time = $startTime;
        $activity->end_time = $endTime;

        if(!empty($data['province']) && $data['province'])
            $activity->province = $data['province'];
        if(!empty($data['city']) && $data['city'])
            $activity->city = $data['city'];
        if(!empty($data['area']) && $data['area'])
            $activity->area = $data['area'];
        if(!empty($data['addressDetail']) && $data['addressDetail'])
            $activity->address_detail = $data['addressDetail'];
        if(!empty($data['rePerson']) && $data['rePerson'])
            $activity->re_person = $data['rePerson'];

            $activity->user_id    = $user->id;

            $activity->classify_id = !empty($data['classifyId']) ? $data['classifyId'] : 0;

            $activity->is_choice = !empty($data['isChoice']) ? $data['isChoice'] : 0;

            $activity->is_set_seat = !empty($data['isSetseat']) ? $data['isSetseat'] : 0;

            $activity->is_vip_free = !empty($data['isVipfree']) ? $data['isVipfree'] : 0;

        if(!empty($data['theme']) && $data['theme'])
            $activity->theme = $data['theme'];
        if(!empty($data['desc']) && $data['desc'])
            $activity->desc = $data['desc'];
        if(!empty($data['detail']) && $data['detail'])
            $activity->detail = $data['detail'];

        $activity->cost = !empty($data['cost']) ? $data['cost'] : 0;

        $activity->accommodation_cost = $accommodationCost;

        $activity->is_guest = !empty($data['isGuest']) ? $data['isGuest'] : 0;

        $activity->guest_cost = !empty($data['guestCost']) ? $data['guestCost'] : 0;

        $activity->img_id = !empty($data['imgId']) ? $data['imgId'] : 0;

        $activity->re_person_phone = !empty($data['rePersonPhone']) ? $data['rePersonPhone'] : 0;

        $activity->max_join_num = !empty($data['maxJoinNum']) ? $data['maxJoinNum'] : 0;

        $activity->is_allow_not_vip_join = !empty($data['isAllowNotVipJoin']) ? $data['isAllowNotVipJoin'] : 0;

        $activity->sort = !empty($data['sort']) ? $data['sort'] : 0;

        $activity->integral_rule_id = $integralRuleId;

        $activity->status        = 1;
        $activity->save();

//        if($integralRule){
//            $newIntegralConductNum = new IntegralConductNumRecord();
//            $newIntegralConductNum->integral_rule_id        = $integralRuleId;
//            $newIntegralConductNum->activity_id             = $activity->id;
//            $newIntegralConductNum->conduct_time            = $activity->start_time;
//            $newIntegralConductNum->save();
//        }
        return response()->json(['level' => 'success','message' => '会议修改成功']);
    }

        //查看活动报名人数
    public function signuplist(Request $request){
        $data = $request->all();
        if(empty($data['id']) || !$data['id'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        $id = $data['id'];
        /** @var Activity $activity_info */
        $activity_info = Activity::where('id',$id)->select('*')->first();
        if(!$activity_info){
            return json_encode(['level' => 'error','message' => '会议已失效']);
        }

        $db = DB::table('user_activity as a')
            ->leftJoin('user_info as b', 'a.user_id', '=', 'b.user_id')
            ->where('a.activity_id',$activity_info->id)
            ->where('a.status','<>',0)
            ->whereNull('a.deleted_at')
            ->whereNull('b.deleted_at');


        $page = $request->has('page')?$request->input('page'):1;
        $pagesize = $request->has('pagesize')?$request->input('pagesize'):16;
        $isSign = $request->has('isSign')?$request->input('isSign'):'';
        $isWithPeople = $request->has('isWithPeople')?$request->input('isWithPeople'):'';
        $realname = $request->has('realname')?$request->input('realname'):'';
        $phone = $request->has('phone')?$request->input('phone'):'';
        $cardNumber = $request->has('cardNumber')?$request->input('cardNumber'):'';
        if($isSign){
            $db = $db->where('a.is_sign',$isSign);
        }
        if($isWithPeople){
            $db = $db->where('a.is_with_people',$isWithPeople);
        }
        if($realname){
            $db = $db->where('b.realname', 'like', '%' . $realname . '%');
        }
        if($phone){
            $db = $db->where('b.phone', 'like', '%' . $phone . '%');
        }
        if($cardNumber){
            $db = $db->where('b.vip_card_number', 'like', '%' . $cardNumber . '%');
        }
        $arr = [
            'page'=>$page,
            'pagesize'=>$pagesize,
            'realname'=>$realname,
            'phone'=>$phone,
        ];
        $user_activity_list = $db->select('a.*','b.realname','b.sexual')->paginate($pagesize);
        $lastPage = $user_activity_list->lastPage();
        $total = $user_activity_list->total();
        $hasMorePages = $user_activity_list->hasMorePages();
        $nextPageUrl = $user_activity_list->nextPageUrl();
        $prvePageUrl = $user_activity_list->previousPageUrl();
        $arr['lastPage'] = $lastPage;
        $arr['total'] = $total;
        $arr['lastPage'] = $lastPage;
        $arr['hasMorePages'] = $hasMorePages;
        $arr['nextPageUrl'] = $nextPageUrl;
        $arr['prvePageUrl'] = $prvePageUrl;
        $user_activity_list = $user_activity_list->all();
        if($user_activity_list){
            foreach($user_activity_list as $key=>$val){
                $cost = $activity_info->cost;
                $is_cost = $cost;

                $val->cost = $cost;
                $val->guest_count = UserActivityGuest::where('activity_id',$id)->where('user_id',$val->user_id)->count();
                $val->is_cost = $is_cost;

                if($val->status == 1){
                    $val->payment_status = 1;
                }else{
                    $val->payment_status = 0;
                }

                $hospitalId = 0;
                $depId = 0;

                $sex = $val->sexual;

                $hospitalName = $val->hospital;

                $depName = $val->dep;

                $val->hospital_id = $hospitalId;
                $val->hospital_name = $hospitalName;
                $val->dep_id = $depId;
                $val->dep_name = $depName;
                $val->sex = $sex;
            }
        }
        $arr = [
            'user_activity_list'=>$user_activity_list,
        ];
        return response()->json(['level' => 'success','message' => '获取信息成功','data'=>$arr]);
    }

    /**
     * 改变报名状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function activityUpdateStatus(Request $request){
        $data = $request->all();
        $id = !empty($data['id']) ? $data['id'] : 0;

        /** @var UserActivity $userActivity */
        $userActivity = UserActivity::whereId($id)->first();
        if(!$userActivity)
            return response()->json(['level' => 'error','message' => '报名信息不存在']);

        if($userActivity->status == 1)
            return response()->json(['level' => 'error','message' => '您已经成功报名了']);

        if($userActivity->status == 0)
            return response()->json(['level' => 'error','message' => '系统错误']);

        $userActivity->status = 1;
        $userActivity->save();

        return response()->json(['level' => 'success','message' => '缴费成功','data' => $userActivity]);
    }

    //查看报名用户详情
    public function signupDetails(Request $request){
        $data = $request->all();
        if(empty($data['id']) || !$data['id'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        $id = $data['id'];
        $user_activity_info = UserActivity::where('id',$id)->select('*')->first();
        if(!$user_activity_info)
            return json_encode(['level' => 'error','message' => '报名记录不存在']); 
        $activityId = $user_activity_info->activity_id;
        $uid = $user_activity_info->user_id;
        $activity_info = Activity::where('id',$activityId)->select('*')->first();
        if(!$activity_info)
            return json_encode(['level' => 'error','message' => '会议已失效']);
        $user_vip_info = VipInfo::where('user_id',$uid)->first();
        if(!$user_vip_info)
            return json_encode(['level' => 'error','message' => '用户信息异常']); 
        $cost = $activity_info->cost;
        $isCost = $cost;
        $user_activity_info->cost = $activity_info->cost;
        $user_activity_info->is_cost = $isCost;
                // print_r($arr);exit;
        return response()->json(['level' => 'success','message' => '获取信息成功','data'=>$user_activity_info]);
    }

     //编辑活动座位号
    public function updateSeatNum(Request $request){
        $data = $request->all();
        if(empty($data['id']) || !$data['id'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        if(empty($data['seatNum']) || !$data['seatNum'])
            return json_encode(['level' => 'error','message' => '网络异常']);
        $id = $data['id'];
        $seatNum = $data['seatNum'];
        /** @var UserActivity $userActivity */
        $userActivity = UserActivity::where('id','<>',$id)->where('seat_num',$seatNum)->select('*')->first();
        if($userActivity)
            return json_encode(['level' => 'error','message' => '座位号已存在']);

        /** @var UserActivity $userActivity */
        $userActivity = UserActivity::where('id',$id)->select('*')->first();
        if(!$userActivity)
            return json_encode(['level' => 'error','message' => '报名记录不存在']);

        /** @var Activity $activity */
        $activity = Activity::whereId($userActivity->activity_id)->whereStatus(1)->first();
        if(!$activity)
            return response()->json(['level' => 'error','message' => '会议不存在']);

        if($activity->is_set_seat == 0)
            return response()->json(['level' => 'error','message' => '该会议不允许安排座位']);

        $userActivity->seat_num = $seatNum;
        $userActivity->save();
        return json_encode(['level' => 'success','message' => '修改成功']);
    }

//    public function activityEndTime(Request $request){
//        /** @var Activity $activity */
//        $activityList = Activity::whereStatus(1)->where('end_time','<=',date("Y-m-d H:i:s",time()))->get();
//        if(count($activityList) > 0){
//            foreach($activityList as $activity){
//                /** @var IntegralConductNumRecord $integralConductNumRecord */
//                $integralConductNumRecord = IntegralConductNumRecord::whereActivityId($activity->id)->whereStatus(0)->first();
//                if($integralConductNumRecord){
//                    $integralConductNumRecord->status = 1;
//                    $integralConductNumRecord->save();
//                }
//            }
//        }
//
//        return $activityList;
//    }
}
